<template>
  <SvgFilters />
  <button
    class="btn glass-effect"
    :class="[
      `btn-${variant}`,
      `btn-${size}`,
      { 'btn-block': block },
      { 'btn-disabled': disabled || loading },
      { 'btn-loading': loading }
    ]"
    :type="type"
    :disabled="disabled || loading"
    @click="handleClick"
    :aria-busy="loading"
  >
    <span v-if="loading" class="loading-spinner"></span>
    <span class="btn-content" :class="{ 'loading-content': loading }">
      <slot>按钮</slot>
    </span>
  </button>
</template>

<script setup>
import SvgFilters from './SvgFilters.vue'

const props = defineProps({
  // 按钮类型：primary, secondary, success, danger, warning, info, light, dark
  variant: {
    type: String,
    default: 'primary',
    validator: (value) => [
      'primary', 'secondary', 'success', 'danger', 
      'warning', 'info', 'light', 'dark'
    ].includes(value)
  },
  // 按钮大小：sm, md, lg
  size: {
    type: String,
    default: 'md',
    validator: (value) => ['sm', 'md', 'lg'].includes(value)
  },
  // 是否阻塞整个宽度
  block: {
    type: Boolean,
    default: false
  },
  // 是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  // 是否显示加载状态
  loading: {
    type: Boolean,
    default: false
  },
  // 原生按钮类型
  type: {
    type: String,
    default: 'button',
    validator: (value) => ['button', 'submit', 'reset'].includes(value)
  }
})

const emit = defineEmits(['click'])

const handleClick = (event) => {
  emit('click', event)
}
</script>

<style scoped>
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-weight: 500;
  border: none;
  border-radius: 0.375rem;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
  outline: none;
  font-size: 1rem;
  font-family: inherit;
  position: relative;
  overflow: hidden;
  background: transparent;
  color: inherit;
}

/* 玻璃拟态效果 - 使用SVG滤镜实现液态玻璃 */
.glass-effect {
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  filter: url(#liquidGlass);
}

.glass-effect:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.4);
}

/* 深色模式下的玻璃效果 */
[data-theme="dark"] .glass-effect {
  background: rgba(31, 41, 55, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.3);
  filter: url(#liquidGlass);
}

[data-theme="dark"] .glass-effect:hover:not(:disabled) {
  background: rgba(31, 41, 55, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

/* 尺寸变体 */
.btn-sm {
  padding: 0.375rem 0.75rem;
  font-size: 0.875rem;
}

.btn-md {
  padding: 0.5rem 1rem;
  font-size: 1rem;
}

.btn-lg {
  padding: 0.625rem 1.25rem;
  font-size: 1.125rem;
}

/* 阻塞宽度 */
.btn-block {
  width: 100%;
}

/* 样式变体 - 与LiquidGlass配合 */
.btn-primary {
  color: white;
}

.btn-primary:hover:not(:disabled) {
  color: white;
}

.btn-secondary {
  color: white;
}

.btn-secondary:hover:not(:disabled) {
  color: white;
}

.btn-success {
  color: white;
}

.btn-success:hover:not(:disabled) {
  color: white;
}

.btn-danger {
  color: white;
}

.btn-danger:hover:not(:disabled) {
  color: white;
}

.btn-warning {
  color: white;
}

.btn-warning:hover:not(:disabled) {
  color: white;
}

.btn-info {
  color: white;
}

.btn-info:hover:not(:disabled) {
  color: white;
}

.btn-light {
  color: #374151;
}

.btn-light:hover:not(:disabled) {
  color: #374151;
}

.btn-dark {
  color: white;
}

.btn-dark:hover:not(:disabled) {
  color: white;
}

/* 禁用状态 */
.btn-disabled {
  opacity: 0.6;
  cursor: not-allowed;
  pointer-events: none;
}

/* 加载状态 */
.btn-loading .btn-content {
  opacity: 0;
}

.loading-spinner {
  position: absolute;
  width: 1rem;
  height: 1rem;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 0.8s linear infinite;
}

/* 深色主题下的加载动画调整 */
.btn-light .loading-spinner {
  border-color: rgba(0, 0, 0, 0.3);
  border-top-color: #374151;
}

/* 加载动画 */
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .btn {
    font-size: 0.875rem;
  }
  
  .btn-lg {
    padding: 0.5rem 1rem;
    font-size: 1rem;
  }
}

/* 焦点状态 */
.btn:focus-visible {
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.3);
}

.btn-primary:focus-visible {
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.5);
}

.btn-secondary:focus-visible {
  box-shadow: 0 0 0 3px rgba(107, 114, 128, 0.5);
}

.btn-success:focus-visible {
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.5);
}

.btn-danger:focus-visible {
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.5);
}

.btn-warning:focus-visible {
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.5);
}

.btn-info:focus-visible {
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
}

.btn-light:focus-visible {
  box-shadow: 0 0 0 3px rgba(243, 244, 246, 0.8);
  border-color: #4f46e5;
}

.btn-dark:focus-visible {
  box-shadow: 0 0 0 3px rgba(31, 41, 55, 0.5);
}
</style>